<!DOCTYPE html>
<meta charset="UTF-8">
<form id="myselectbook" style="margin-top: 20px">
	<div class="layui-form-item">
		<div class="layui-inline">
			<label class="layui-form-label">编号：</label>
			<div class="layui-input-inline">
				<input type="text" name="byid" id="byid" class="layui-input"  placeholder="请输入编号"/>
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">书名：</label>
			<div class="layui-input-inline">
				<input type="text" name="title" id="title" class="layui-input" placeholder="请输入书名"/>
			</div>
		</div>

		<div class="layui-inline">
			<div class="layui-btn-group ">
				<button type="button" class="layui-btn layui-icon layui-icon-search"onclick="searchbook()">查询</button>
				<button type="button" class="layui-btn layui-icon layui-icon-circle"onclick="emptybook()">清空</button>
			</div>
		</div>
	</div>
</form>
<script type="text/html" id="toolbar">
	<div class="layui-btn-group ">
   		<button class="layui-btn layui-icon layui-icon-add-1" lay-event="addbook"> 新增</button>
  	  	<button class="layui-btn layui-icon layui-icon-delete" lay-event="deletebook"> 删除</button>
	</div>
</script>
<table id="book_table" lay-filter="test"></table>
<script>
layui.use('table', function(){
  var table = layui.table;
  table.render({
	  	title:'学生列表',
	  	color:'#666',
		height: 'auto',
	    elem: '#book_table',
	    url:'book/page',
	    toolbar: '#toolbar',
	    page: true,
	    cols: [
	    	[
	      {type:'checkbox'},
	      {field:'byid', width:'16%', title: '图书编号'},
	      {field:'title', width:'16%', title: '图书名称'},
	      {field:'suthor', width:'16%', title: '作者'},
	      {field:'isbn', width:'16%', title: 'ISBN'},
	      {field:'number', width:'16%', title: '数量'},
	      {field:'type', width:'16%', title: '类型',templet:function(d){
	    	  return d.type?d.type.type: '';
	      }}
	    ]
	    	]
  		
  });
  table.on('toolbar',function(obj){
	  	switch(obj.event){
	      	case 'addbook':
	      		add();
	      		break;
	      	case 'updatebook':
	      		update();
	      		break;
	      	case 'deletebook':
	      		del();
	      		break;
	  	}
	  });
	  function add(){
			layer.open({
		   	type:2,
		   	title:'<i class="layui-icon">新增</i>',
		   	area: ['500px','550px'],
		   	content:['book/edit'],
		   	btn:['提交','退出'],
		   	yes : function(index, layero) {
		   		var obj = layero.find("iframe")[0].contentWindow;
				var win = top.window['layui-layer-iframe' + index];
		   		var file = win.$('#file')[0].files[0];
		   		var type = win.$('#type').val();
		   		var byid = win.$('#byid').val();
		   		var title = win.$('#title').val();
		   		var suthor = win.$('#suthor').val();
		   		var number = win.$('#number').val();
		   		var isbn = win.$('#isbn').val();
		   		var formData = new FormData();
        		formData.append("files", file);
        		formData.append("type", type);
        		formData.append("byid", byid);
        		formData.append("title", title);
        		formData.append("suthor", suthor);
        		formData.append("number", number);
        		formData.append("isbn", isbn);
		   		$.ajax({
		   			type:"post",   
		   			url:"book/save",
		   			data: formData,
		   		    processData: false,
		   		    contentType: false,
		   			mimeType:"multipart/form-data",
		   		 	success:function(data){
		   		 		layer.msg(data);
		   		 		red();
		   		 	}
		   		});
		   		layer.closeAll('iframe');
			} 
		  })
		}
	  function update(){
		  var checkStatus = table.checkStatus('book_table');
		  var data = checkStatus.data;
		  var zid = data.length==1?data[0].id: '';
		  if(data.length!=1){
			  layer.msg("请选择一行进行修改！");
			  return;
		  }
		  layer.open({
			  type:2,
		    	title:'<i class="layui-icon">修改</i>',
		    	area: ['500px','550px'],
		    	content:['book/edit?id='+zid],
		    	btn:['提交','退出'],
		    	yes : function(index, layero) {
		    		var obj = layero.find("iframe")[0].contentWindow;
					var win = top.window['layui-layer-iframe' + index];
			   		var file = win.$('#file')[0].files[0];
			   		var id = win.$("#id").val();
			   		var byid = win.$('#byid').val();
			   		var title = win.$('#title').val();
			   		var suthor = win.$('#suthor').val();
			   		var number = win.$('#number').val();
			   		var isbn = win.$('#isbn').val();
			   		var formData = new FormData();
	        		formData.append("id", id);
	        		formData.append("files", file);
	        		formData.append("byid", byid);
	        		formData.append("title", title);
	        		formData.append("suthor", suthor);
	        		formData.append("number", number);
	        		formData.append("isbn", isbn);
	        		$.ajax({
			   			type:"post",   
			   			url:"book/save",
			   			data: formData,
			   		    processData: false,
			   		    contentType: false,
			   			mimeType:"multipart/form-data",
			   		 	success:function(data){
			   		 		layer.msg(data);
			   		 		red();
			   		 	}
			   		});
					layer.closeAll('iframe');
				}
		  })
	  }
	  
	function del(){
		 var checkStatus = table.checkStatus('book_table');
		  var data = checkStatus.data;
		  var zid = data.length==1?data[0].id: '';
		  if(data.length!=1){
			  layer.msg("请选择一行进行删除！");
			  return;
		  }
		  layer.confirm('确定删除此行数据吗？', function() {
			  $.post('book/delete',{id:zid},function(result){
				  layer.msg(result);
				  red();
			  })
		  }); 
	}
	  
});
function red(){
	var table = layui.table;
  	table.reload('book_table', {
	  url: 'book/page'
	});
}
//查询
function searchbook(){
	var title = $('#title').val();
	var byid = $('#byid').val();
	layui.use('table', function(){
		var table = layui.table;
		table.reload('book_table',{
			url:'book/page',
			where: {
				title:title,
				byid:byid
			}
		});
	});
}
//清空表格
function emptybook(){
	$("#myselectbook")[0].reset();
	var title = $('#title').val();
	var byid = $('#byid').val();
	layui.use('table', function(){
		var table = layui.table;
	  	table.reload('book_table', {
		  url: 'book/page',
		  where: {
			  	title:title,
				byid:byid
			}
		});
	});
}
  </script>